package com.luhan.lhframe.db.dao;

import android.content.Context;

import com.luhan.lhframe.db.DBHelper;

/**
 * 操作Test的类，有新增、删除和查询方法
 * 使用方法：直接调用方法即可。
 * created by LuHan at 2016/6/20 下午 5:09
 */
public class TestDao {
    private static final String TAG = "TestDao";
    private String table_name = DBHelper.TABLE_TEST;//表名
    DBHelper helper = null;

    public TestDao(Context cxt) {
        helper = new DBHelper(cxt);
    }

    /**
     * 当Activity中调用此构造方法，传入一个版本号时，系统会在下一次调用数据库时调用Helper中的onUpgrade()方法进行更新
     *
     * @param cxt
     * @param version
     */
    public TestDao(Context cxt, int version) {
        helper = new DBHelper(cxt, version);
    }

    // 插入操作
   /* public boolean insertData(BuildingBean bean) {
        long isSucceed;
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("stmc", bean.getStmc());
        isSucceed = db.insert(table_name, null, values);
        db.close();
        return isSucceed > 0;
    }*/

    /**
     * 查询操作 (查询所有)
     * @return
     */
    /*public List<BuildingBean> findData() {
        List<BuildingBean> list = new ArrayList<BuildingBean>();
        String sql = "select * from " + table_name;
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            BuildingBean bean = new BuildingBean();
            //String name = cursor.getString(0); //获取第一列的值,第一列的索引从0开始
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String stmc = cursor.getString(cursor.getColumnIndex("stmc"));

            bean.setBuildingId(String.valueOf(id));
            bean.setStmc(stmc);

            list.add(bean);
        }
        cursor.close();
        db.close();
        return list;
    }*/

    /**
     * 查询操作 (根据实体名称查询)
     *
     * @param keyWord
     * @return
     */
    /*public List<BuildingBean> findDataByStmc(String keyWord) {
        List<BuildingBean> list = new ArrayList<BuildingBean>();
        String sql = "select * from " + table_name + " where (stmc like '%" + keyWord + "%')";
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            BuildingBean bean = new BuildingBean();
            //String name = cursor.getString(0); //获取第一列的值,第一列的索引从0开始
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String stmc = cursor.getString(cursor.getColumnIndex("stmc"));

            bean.setBuildingId(String.valueOf(id));
            bean.setStmc(stmc);
            list.add(bean);
        }
        cursor.close();
        db.close();
        return list;
    }*/

    /**
     * 删除操作 (根据buildingId删除)
     * @param id
     * @return
     */
    /*public boolean deleteById(String id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int isDelete;
        String[] args;
        args = new String[]{id};
        isDelete = db.delete(table_name, "id=?", args);
        return isDelete > 0;
    }*/
}
